package com.gidgabs.modules.api.controller;

import java.util.Date;

import com.gidgabs.common.annotation.AuthIgnoreApi;
import com.gidgabs.common.utils.*;
import com.gidgabs.dto.TbProductDto;
import com.gidgabs.modules.api.service.*;
import com.gidgabs.modules.entity.*;
import com.gidgabs.utils.OrderUtil;
import com.gidgabs.utils.SessionDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 产品表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-07-02 11:03:07
 */
@Api("产品功能模块")
@RestController
@RequestMapping("/api/product")
public class TbProductController {
    @Autowired
    private TbProductService productService;

    @Autowired
    private TbProductCollectService tbProductCollectService;

    @Autowired
    private TbProductReadService tbProductReadService;

    @Autowired
    private TbProductRewardService tbProductRewardService;

    @Autowired
    private TbUserService userService;

    /**
     * 查询我的产品列表 根据创建时间排序
     *
     * @param pageSize
     * @param pageNo
     * @return
     */
    @ApiOperation(value = "查询我的产品列表 根据创建时间排序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @GetMapping("getMyList")
    public ReturnJson<TbProductDto> getMyList(Integer pageSize, Integer pageNo) {
        //获取用户信息
        TbUser tbUser = new TbUser();
        tbUser.setUserId(SessionDataUtil.getCurrentUserId());
        TbUser user = userService.queryObject(tbUser);
        if (user == null) {
            return ReturnJson.fail("登录用户不存在！");
        }
        return productService.queryMyProductList(pageSize, pageNo, user);
    }

    /**
     * 根据产品id查询产品信息 并获取收藏数...
     *
     * @param productId
     * @return
     */
    @ApiOperation(value = "根据产品id查询产品信息 并获取收藏数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "产品id", dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @PostMapping("/info/{productId}")
    public ReturnJson<TbProductDto> info(@PathVariable("productId") Integer productId) {
        //获取用户信息
        TbUser tbUser = new TbUser();
        tbUser.setUserId(SessionDataUtil.getCurrentUserId());
        TbUser user = userService.queryObject(tbUser);
        if (user == null) {
            return ReturnJson.fail("登录用户不存在！");
        }
        //根据登录用户添加阅读数
        TbProductReadEntity productReadEntity = new TbProductReadEntity();
        productReadEntity.setUserIdMaster(user.getUserId());
        productReadEntity.setReadIdSlave(productId);
        //如果为空说明没有看过该信息
        if (tbProductReadService.queryByObject(productReadEntity) == null) {
            //添加一条阅读记录
            productReadEntity.setCreateTime(new Date());
            tbProductReadService.save(productReadEntity);
        }
        TbProductEntity product = productService.selectTbProductById(productId);
        TbProductDto tbProductDto = product.toDto();
        //查询收藏数
        TbProductCollectEntity tbProductCollectEntity = new TbProductCollectEntity();
        tbProductCollectEntity.setProductIdSlave(productId);
        int collectNumber = tbProductCollectService.queryCount(tbProductCollectEntity);
        tbProductDto.setCollectNumber(collectNumber);
        //查询阅读数
        TbProductReadEntity tbProductReadEntity = new TbProductReadEntity();
        tbProductReadEntity.setReadIdSlave(productId);
        int readNumber = tbProductReadService.queryCount(tbProductReadEntity);
        tbProductDto.setReadNumber(readNumber);
        //查询打赏数
        TbProductRewardEntity tbProductRewardEntity = new TbProductRewardEntity();
        tbProductRewardEntity.setRewardIdSlave(productId);
        int rewardNumber = tbProductRewardService.queryCount(tbProductRewardEntity);
        tbProductDto.setRewardNumber(rewardNumber);
        //查询转发数
        int forwardNumber = productService.selectForwardNumber(tbProductDto.getProductCode());
        tbProductDto.setForwardNumber(forwardNumber);
        return ReturnJson.success("查询成功", tbProductDto);
    }

    /**
     * 保存
     *
     * @param product
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存产品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    public ReturnJson save(@ModelAttribute TbProductDto product) {
        //获取用户信息
        TbUser tbUser = new TbUser();
        tbUser.setUserId(SessionDataUtil.getCurrentUserId());
        TbUser user = userService.queryObject(tbUser);
        if (user == null) {
            return ReturnJson.fail("登录用户不存在！");
        }
        //如何有id就做修改操作
        if(product.getProductId() != null){
            product.setUpdateTime(new Date());
            productService.update(product.toEntity());
        }else{
            //初始化
            product.setUserId(user.getUserId());
            product.setProductCode(OrderUtil.getScanNumber());
            product.setDeleteType(DeleteType.NOT_DELETE.getKey());
            product.setCreateTime(new Date());
            product.setUpdateTime(new Date());
            productService.save(product.toEntity());
        }
        return ReturnJson.success("保存成功");
    }

    /**
     * 根据广告类型查询对应的广告和价格排序 （分页）
     *
     * @param pageSize
     * @param pageNo
     * @param fromTypeId
     * @return
     */
    @AuthIgnoreApi
    @ApiOperation(value = "根据广告价格排序查询广告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "fromTypeId", value = "广告类型 1 personal 私人  2 education 教育 3 fashion 时尚  4 design 设计 5 software 软件", dataType = "int", paramType = "query")
    })
    @GetMapping("/list")
    public ReturnJson<TbProductDto> getAdvertiseList(Integer pageSize, Integer pageNo, Integer fromTypeId) {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        if (fromTypeId == null) {
            fromTypeId = 1;
        }
        return productService.queryAdvertiseList(pageSize, pageNo, fromTypeId);
    }

    /**
     * 添加转发数
     *
     * @param productId
     * @return
     */
    @PostMapping("/addForWard")
    @ApiOperation(value = "添加转发数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "被转发产品id", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    public ReturnJson<String> addForWard(Integer productId) {
        //获取用户信息
        TbUser tbUser = new TbUser();
        tbUser.setUserId(SessionDataUtil.getCurrentUserId());
        TbUser user = userService.queryObject(tbUser);
        if (user == null) {
            return ReturnJson.fail("登录用户不存在");
        }
        TbProductEntity tbProductEntity = productService.selectTbProductById(productId);
        TbProductEntity productEntity = new TbProductEntity();
        //"前付后" tbProductEntity对象属性值 赋值给 productEntity对象
        BeanUtils.copyProperties(tbProductEntity, productEntity);
        //初始化id 封装转发人即当前用户
        productEntity.setProductId(null);
        productEntity.setUserId(user.getUserId());
        productEntity.setSourceUserId(tbProductEntity.getUserId());
        productService.save(productEntity);
        return ReturnJson.success("转发成功");
    }

    /**
     * 删除我的商品 实质做逻辑删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除我的商品 实质做逻辑删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "被产品id集", dataType = "int[]", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    public ReturnJson<String> delete(Integer[] ids) {
        productService.updateBatch(ids);
        return ReturnJson.success("删除成功");
    }

}
